﻿#include "TextViewItemDelegate.h"
#include <QStyleOptionViewItem>

#include "LumDebug.h"
using BaseClass = QStyledItemDelegate;

#include <QLabel>
#include <QPainter>

struct TextViewItemDelegate::TextViewItemDelegateData {
    QColor highlightTextColor;
    int highlightTextColorRole;
    QScopedPointer<QLabel> label;
};

TextViewItemDelegate::~TextViewItemDelegate() { }

TextViewItemDelegate::TextViewItemDelegate(QObject *parent) : BaseClass{parent} {
    d.reset(new TextViewItemDelegateData);
    d->label.reset(new QLabel);
}

void TextViewItemDelegate::setHighlightTextColor(const QColor &color, int dataRole) {
    d->highlightTextColor = color;
    d->highlightTextColorRole = dataRole;
}

QColor TextViewItemDelegate::highlightTextColor() const {
    return d->highlightTextColor;
}

void TextViewItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
    //BaseClass::paint(painter, option, index);
    const auto &text = index.data(Qt::DisplayRole).toString();
    const auto &hightlightText = index.data(d->highlightTextColorRole).toString();

    R"("
    <html><head/><body><p>
    AB
    <span style=" color:#ff5500;">CD</span>
    E
    <span style=" color:#55aa7f;">FG</span>
    H
    </p></body></html>
    )";

    const bool focused = (option.state & QStyle::State_HasFocus);
    const bool selected = (option.state & QStyle::State_Selected);
    const bool active = (option.state & QStyle::State_Active);
    const bool hover = (option.state & QStyle::State_MouseOver);

    DEBUG << index.data().toString();
    DEBUG << "focused = " << focused;
    DEBUG << "hover = " << hover;
    DEBUG << "\n";

    const auto &border_color = focused ? "#99D1FF" : "transparent";
    const auto &border_color_left = option.viewItemPosition == QStyleOptionViewItem::End ? "transparent" : border_color;
    const auto &border_color_right = option.viewItemPosition == QStyleOptionViewItem::Beginning ? "transparent" : border_color;
    auto background_color = "transparent";
    if(selected) {
        background_color = (active || focused) ? "#CDE8FF" : "#D9D9D9";
    }

    QString qss = QString("QLabel {"
                          "    border: 1px solid %1;"
                          "    border-left-color: %2;"
                          "    border-right-color: %3;"
                          "    background-color: %4;"
                          "}")
            .arg(border_color_left)
            .arg(border_color_right)
            .arg(border_color)
            .arg(background_color);

    QString html = "<html><head/><body><p>";
    if(hightlightText.contains(hightlightText)) {
        const auto &textItems = text.split(hightlightText);
        const auto &item_count = textItems.count();
        for(int i = 0; i < item_count; ++i) {
            const auto &item = textItems.at(i);
            html += item;
            if(i<item_count-1) {
                html += QString("<b style=\" color:%1;\">").arg(d->highlightTextColor.name(QColor::HexRgb));
                html += hightlightText;
                html +="</b>";
            }
        }
    } else {
        html += text;
    }
    html += "</p></body></html>";
    const auto label = d->label.data();
    label->setStyleSheet(qss);
    label->setFont(option.font);
    label->setPalette(option.palette);
    label->setTextFormat(Qt::RichText);
    label->resize(option.rect.size());
    label->setText(html);
    painter->drawPixmap(option.rect, label->grab());
}
